package org.opens.excel.controller;

import org.opens.util.ExcelUtils;
import org.opens.excel.entity.EasyPoiEntity;
import org.opens.excel.service.EasyPoiEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping(value = "/easyPoi")
@RestController
@CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true")
public class EasyPoiController {

    @Autowired
    private EasyPoiEntityService easyPoiEntityService;

    @RequestMapping(value = "/export")
    public void export(EasyPoiEntity easyPoiEntity, HttpServletResponse response) {
        List<EasyPoiEntity> easyPoiEntityList = easyPoiEntityService.getEasyPoiEntityNyNum(10);
        try {
            if (easyPoiEntity != null && easyPoiEntity.getId() != null) {
                easyPoiEntityList = easyPoiEntityList.stream().filter(x -> x.getId() > easyPoiEntity.getId()).collect(Collectors.toList());
            }
            ExcelUtils.exportExcel(easyPoiEntityList, "测试导出", "测试sheet", EasyPoiEntity.class, "测试导出文件", true, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
